package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;

import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController("UserOrderController")
@RequestMapping("/user/order")
@Slf4j
public class OrderController {

    @Resource
    private OrderService orderService;

    // 用户下单
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO)
    {
        return Result.success(orderService.submit(ordersSubmitDTO));

    }
    //历史订单分页查询
    @GetMapping("/historyOrder")
    public Result<PageResult> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO)
    {
        PageResult pageResult = orderService.pageQuery(ordersPageQueryDTO);

        return Result.success(pageResult);
    }
    // 订单详情
    @GetMapping("/details/{id}")
    public Result<OrderVO> details(@PathVariable Long id)
    {
        OrderVO orderVO = orderService.details(id);
        return Result.success(orderVO);
    }
    //取消订单
   @PutMapping("/cancel/{id}")
    public Result cancelOrder(@PathVariable Long id)
   {
       orderService.cancelOrderById(id);
       return Result.success();
   }
    //再来一单
    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable Long id)
    {
        orderService.repetition(id);
        return Result.success();
    }


    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    //催单
    @GetMapping("reminder/{id}")
    private Result reminder(@PathVariable Long id){

        orderService.reminder(id);
        return Result.success();

    }
}
